package edu.zjut.hjg.saas.biz.service.impl;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import edu.zjut.hjg.saas.biz.dao.UserDao;
import edu.zjut.hjg.saas.biz.dao.UserLogDao;
import edu.zjut.hjg.saas.biz.model.User;
import edu.zjut.hjg.saas.biz.model.UserLog;
import edu.zjut.hjg.saas.biz.service.UserService;
import edu.zjut.hjg.saas.biz.service.exception.ServiceException;
import edu.zjut.hjg.saas.biz.support.OperateType;
import edu.zjut.hjg.saas.biz.support.RoleName;
import edu.zjut.hjg.saas.biz.support.ServiceError;
import edu.zjut.hjg.saas.common.util.MD5Util;

public class UserServiceImpl implements UserService {
    private static final Log log = LogFactory.getLog(UserServiceImpl.class);

    private UserDao          userDao;
    private UserLogDao       userLogDao;

    public void setUserDao(UserDao userDao) {
        this.userDao = userDao;
    }

    public void setUserLogDao(UserLogDao userLogDao) {
        this.userLogDao = userLogDao;
    }

    @Override
    public User authenticate(int appId, String loginId, String password) {
        User user = userDao.findByAppIdAndLoginId(appId, loginId);
        if (user != null && user.getPassword().equals(MD5Util.toMD5(password))) {
            return user;
        }
        return null;
    }

    @Override
    public Integer create(User user, String operator) throws ServiceException {
        if (this.isLoginIdExist(user.getLoginId(), user.getKindergarten().getId())) {
            String message = "create user error, loginId Duplicate: loginId=" + user.getLoginId();
            log.error(message);
            throw new ServiceException(message, ServiceError.LOGIN_ID_DUPLICATE);
        }
        // 密码进行MD5哈希
        user.setPassword(MD5Util.toMD5(user.getPassword()));
        Integer id = userDao.create(user);
        UserLog userLog = new UserLog();
        userLog.setKindergarten(user.getKindergarten());
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.INSERT);
        userLog.setDescription("添加了一个教师账号:" + user.getLoginId());
        userLogDao.create(userLog);
        return id;
    }

    @Override
    public boolean isLoginIdExist(String loginId, int appId) {
        return userDao.findByAppIdAndLoginId(appId, loginId) != null;
    }

    @Override
    public void update(int id, String loginId, String name, int appId, String operator)
            throws ServiceException {
        User user = userDao.findByIdAndAppId(id, appId);
        if (user == null) {
            throw new RuntimeException("no user found for id=" + id + " and appId=" + appId);
        }
        User userTmp = userDao.findByAppIdAndLoginId(appId, loginId);
        if (userTmp != null && !userTmp.getId().equals(user.getId())) {
            String message = "update user error, loginId Duplicate: loginId=" + loginId;
            log.error(message);
            throw new ServiceException(message, ServiceError.LOGIN_ID_DUPLICATE);
        }
        user.setName(name);
        user.setLoginId(loginId);
        userDao.update(user);
        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.UPDATE);
        userLog.setDescription("修改了一个教师账号:" + loginId);
        userLogDao.create(userLog);
    }

    @Override
    public void updatePassword(int id, String oldPassword, String newPassword)
            throws ServiceException {
        User user = userDao.loadNotDeleted(id);
        if (user == null) {
            throw new RuntimeException("user not found for id=" + id);
        } else if (!user.getPassword().equals(MD5Util.toMD5(oldPassword))) {
            // 原密码错误
            throw new ServiceException(ServiceError.PASSWORD_NOT_MATCH);
        }
        // 正确
        user.setPassword(MD5Util.toMD5(newPassword));
        userDao.update(user);
    }

    @Override
    public List<User> listByRoleNameAndAppId(RoleName roleName, int appId) {
        return userDao.listByRoleNameAndAppId(roleName, appId);
    }

    @Override
    public void deleteByIdAndAppId(int id, int appId, String operator) {
        User user = userDao.findByIdAndAppId(id, appId);
        if (user == null) {
            throw new RuntimeException("no user found for id=" + id + " and appId=" + appId);
        }
        userDao.deleteByIdAndAppId(id, appId);
        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.DELETE);
        userLog.setDescription("删除了一个教师账号:" + user.getLoginId());
        userLogDao.create(userLog);
    }

    @Override
    public void resetPassword(int id, int appId, String newPassword, String operator) {
        User user = userDao.findByIdAndAppId(id, appId);
        if (user == null) {
            throw new RuntimeException("user not found for id=" + id);
        }
        user.setPassword(MD5Util.toMD5(newPassword));
        userDao.update(user);

        UserLog userLog = new UserLog();
        userLog.getKindergarten().setId(appId);
        userLog.setLoginId(operator);
        userLog.setOperateType(OperateType.UPDATE);
        userLog.setDescription("重置了教师密码:" + user.getLoginId());
        userLogDao.create(userLog);
    }
}
